package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SaveAccountServlet extends HttpServlet {
    public static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";
    public static final String VIEW_EDIT_ACCOUNT_FORM = "/WEB-INF/jsp/account/EditAccountForm.jsp";
    private Account account;
    private String password;
    private String repeatedPassword;
    private AccountService accountService;
    private boolean optionCheck;
    private boolean bannerCheck;
    private LogService logService;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        account = (Account)session.getAttribute("account");
        password = request.getParameter("password");
        repeatedPassword = request.getParameter("repeatedPassword");

        account.setFirstName(request.getParameter("firstName"));
        account.setLastName(request.getParameter("lastName"));
        account.setEmail(request.getParameter("email"));
        account.setPhone(request.getParameter("phone"));
        account.setAddress1(request.getParameter("address1"));
        account.setAddress2(request.getParameter("address2"));
        account.setCity(request.getParameter("city"));
        account.setState(request.getParameter("state"));
        account.setZip(request.getParameter("zip"));
        account.setCountry(request.getParameter("country"));
        account.setLanguagePreference(request.getParameter("languagePreference"));
        account.setFavouriteCategoryId(request.getParameter("favouriteCategoryId"));

        if(request.getParameter("listOption") == null)
            optionCheck = false;
        else
            optionCheck = true;

        if(request.getParameter("bannerOption") == null)
            bannerCheck = false;
        else
            bannerCheck = true;
        account.setListOption(optionCheck);
        account.setBannerOption(bannerCheck);

        //System.out.println(request.getParameter("bannerOption"));
        if (!password.equals(repeatedPassword)){
            session.setAttribute("message","Entered passwords differ.");
            request.getRequestDispatcher(ERROR).forward(request,response);
        } else {
            account.setPassword(password);
        }
        session.setAttribute("account",account);
        accountService = new AccountService();
        accountService.updateAccount(account);
        logService = new LogService();
        logService.insertLog("Change Infomation","Username:"+account.getUsername(), account.getUsername());
        request.getRequestDispatcher(VIEW_EDIT_ACCOUNT_FORM).forward(request,response);
    }
}
